package jsyf.oa.web.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.RegionFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2017/12/7 0007.
 */

@Api(value = "自定义区域接口", description = "自定义区域接口")
@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private RegionFacade regionFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String result = regionFacade.save(jsonData);
        if (!StringUtils.isEmpty(result)) {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", result);
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("添加失败！"));
        }
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = regionFacade.delById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void removeById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = regionFacade.removeById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/getTreeNodes.json", method = RequestMethod.POST)
    public void getTreeNodes(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> result = regionFacade.getTreeNodes();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getRegionByUserId.json", method = RequestMethod.POST)
    public void getRegionByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        JSONObject result = regionFacade.getRegionByUserId(userId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

    @RequestMapping(value = "/getBigRegionListByCompanyId.json", method = RequestMethod.POST)
    public void getBigRegionListByCompanyId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> result = regionFacade.getBigRegionListByCompanyId(jsonParams.getString("companyId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getBigRegionListByUserId.json", method = RequestMethod.POST)
    public void getBigRegionListByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> result = regionFacade.getBigRegionListByUserId(jsonParams.getString("userId"),jsonParams.getString("isBigRegion"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }
    @RequestMapping(value = "/getRegionByWreport.json", method = RequestMethod.POST)
    public void getRegionByWreport(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String result = regionFacade.getRegionByWreport(jsonParams.getString("userId"),jsonParams.getString("roleName"));
        JSONObject object=new JSONObject();
        object.put("roleNames",result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(object));
    }

    @RequestMapping(value = "/getSmallRegionListByParentId.json", method = RequestMethod.POST)
    public void getSmallRegionListByParentId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> result = regionFacade.getBigRegionListByCompanyId(jsonParams.getString("parentId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/sort.json", method = RequestMethod.POST)
    public void sort(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Boolean result = regionFacade.sort(jsonParam.getString("regionIds"));
        JSONObject objResult = new JSONObject();
        objResult.put("success",result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }
}
